package middleware.kafka.init_kafka;

import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author suxiaoxiao
 * @Date 2019/6/30 19:13
 * @Version 1.0
 */
public class ThirdMultiConsumerThreadDemo {

    public static final String brokerList = "47.104.178.113:9092";
    public static final String topic = "topic-demo";
    public static final String groupId = "group.demo";

    public static Properties initConfig() {
        Properties props = new Properties();
        /**
         * 消费者从 broker 端获取的消息格式都是字节数组（byte[]）类型，
         * 所以需要执行相应的反序列化操作才能还原成原有的对象格式。
         */
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        /**
         * 用来指定连接 Kafka 集群所需的 broker 地址清单，
         * 具体内容形式为 host1:port1,host2:post，
         * 可以设置一个或多个地址，
         * 中间用逗号隔开，此参数的默认值为“”。
         */
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList);
        /**
         * 消费者隶属的消费组的名称，默认值为“”。如果设置为空，则会报出异常
         * 一般而言，这个参数需要设置成具有一定的业务意义的名称。
         */
        props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
        /**
         * 这个参数用来设定 KafkaConsumer 对应的客户端id，默认值也为“”。
         * 如果客户端不设置，则 KafkaConsumer 会自动生成一个非空字符串，
         * 内容形式如“consumer-1”、“consumer-2”，
         * 即字符串“consumer-”与数字的拼接。
         */
        props.put(ConsumerConfig.CLIENT_ID_CONFIG, "consumer.client.id.demo");
        /**
         * 关闭自动提交
         *
         */
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
        return props;
    }

    public static void main(String[] args) {

    }

    public static class KafkaConsumerThread extends Thread {
        private KafkaConsumer<String, String> kafkaConsumer;
        // 注意包不要导错
        private ExecutorService executorService;
        private int threadNumber;

        public KafkaConsumerThread(Properties props, String topic, int threadNumber) {
            kafkaConsumer = new KafkaConsumer<>(props);
            // 订阅主题
            kafkaConsumer.subscribe(Collections.singletonList(topic));
            this.threadNumber = threadNumber;
            executorService = new ThreadPoolExecutor(threadNumber, threadNumber,
                    0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(1000),
                    new ThreadPoolExecutor.CallerRunsPolicy());
            // CallerRunsPolicy 如果队列放不下，线程池KafkaConsumerThread 没有可用线程，那么就交给主线程执行，会阻塞主线程。
        }

        @Override
        public void run() {
            try {
                while (true) {
                    ConsumerRecords<String, String> records = kafkaConsumer.poll(Duration.ofMillis(100));
                    if (!records.isEmpty()) {
                        executorService.submit(new RecordsHandler(records));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                kafkaConsumer.close();
            }
        }
    }

    public static class RecordsHandler extends Thread {
        public final ConsumerRecords<String, String> records;
        Map<TopicPartition, OffsetAndMetadata> offsets;

        public RecordsHandler(ConsumerRecords<String, String> records) {
            this.records = records;
        }

        @Override
        public void run () {
            // 处理 records
            for (TopicPartition tp : records.partitions()) {
                List<ConsumerRecord<String, String>> tpRecords = records.records(tp);
                // 处理 tpRecords
                long lastConsumedOffset = tpRecords.get(tpRecords.size() - 1).offset();
                synchronized (offsets) {
                    if (offsets.containsKey(tp)) {
                        offsets.put(tp, new OffsetAndMetadata(lastConsumedOffset + 1));
                    } else {
                        long position = offsets.get(tp).offset();
                        if (position < lastConsumedOffset + 1) {
                            offsets.put(tp, new OffsetAndMetadata(lastConsumedOffset + 1));
                        }
                    }
                }
            }
        }

    }

}
